<?php
class IndexController
{
  	private $param;
  	
  	//构造
  	public function __construct($p='')
  	{
  		 $this->param = $p;
  	}
  	
  	//首页
  	public function  indexAction()	
  	{
  		view('index','index');
  	}
  	
  	public function  registAction()
  	{
  		if(isset($this->param['val']) && $this->param['val']){
  			//实例化模型
            $db= new userModel;
            if($db->checkName(get_magic_quotes_gpc() ? $this->param['val'] : addslashes($this->param['val']))){
            	echo 0;
            }else{
            	echo 1;
            }
            return false;
  		}
  		view('index','regist');
  	}
  	
  	public function checkRegistAction()
  	{
  	      //检测用户名，两次密码输入，邮箱
          if(isset($_POST['username']) && $_POST['username'])
          {
              $username = trim($_POST['username']);
          }else{
              die('用户名不能为空');
          }
          if(!isset($_POST['password']) || !isset($_POST['repassword']) || ($_POST['password'] != $_POST['repassword']))
          {
              die( '两次密码输入不一致');
          }else{
              $password = md5(trim($_POST['password']));
          }
          if(isset($_POST['email']) && $_POST['email'])
          {
              if(preg_match("/^[0-9a-zA-Z]+(?:[_-][a-z0-9-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*.[a-zA-Z]+$/i",trim($_POST['email'])))
              {
                  $email = $_POST['email'];
              }else{
                  die('邮箱格式不正确');
              }
          }
          //实例化模型
          $db= new userModel;
          if($db->regist(array('username'=>$username,'password'=>$password,'email'=>$email)))
          {
              echo '注册成功';
          }else{
              echo '注册失败';
          }
          //分配模版
          include 'application/views/index/registRes.html';
  	}
  	
  	public function loginAction()
  	{
  		include '/application/views/index/login.html';
  	}
}
